package com.zjw.zy.bean;


import android.os.Parcel;
import android.os.Parcelable;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

import com.google.gson.annotations.SerializedName;
import com.zjw.zy.utils.AppUtils;
import com.zjw.zy.utils.GsonUtils;
import com.zjw.zy.utils.TimeUtils;

@Entity(tableName = "wechat_message")
public class WsMessage implements Parcelable {

    public static final String BIND_LOVE_INFO = "BIND_LOVE_INFO?";
    public static final String WECHAT_MESSAGE_INFO = "WECHAT_MESSAGE_INFO?";
    public static final String WECHAT_MESSAGE_PULL_INFO = "WECHAT_MESSAGE_PULL_INFO?";
    //客户端到客户端的消息
    public static final String CLIENT_TO_CLIENT_INFO = "CLIENT_TO_CLIENT_INFO?";
    public static final String CLIENT_TO_CLIENT_FAIL_INFO = "CLIENT_TO_CLIENT_FAIL_INFO?";
    //客户端到客户端强消息（云端会有缓存消息，拉取）
    public static final String CLIENT_TO_CLIENT_STRONG_INFO = "CLIENT_TO_CLIENT_STRONG_INFO?";
    ///客户端到客户端强消息缓存
    public static final String CLIENT_TO_CLIENT_STRONG_CACHE_INFO = "CLIENT_TO_CLIENT_STRONG_CACHE_INFO?";


    @SerializedName("id")
    @PrimaryKey(autoGenerate = true)
    private Long id;
    @SerializedName("from")
    @ColumnInfo(name = "from", typeAffinity = ColumnInfo.TEXT)
    private String from;
    @SerializedName("to")
    @ColumnInfo(name = "to", typeAffinity = ColumnInfo.TEXT)
    private String to;
    @ColumnInfo(name = "text", typeAffinity = ColumnInfo.TEXT)
    @SerializedName("text")
    private String text;
    @ColumnInfo(name = "date", typeAffinity = ColumnInfo.INTEGER)
    @SerializedName("date")
    private Long date;
    @ColumnInfo(name = "info", typeAffinity = ColumnInfo.TEXT)
    @SerializedName("info")
    private String info;

    private transient Object payload;


    public WsMessage() {

    }

    public void jsonPayload() {
        this.text = GsonUtils.toJson(payload == null ? "" : payload);
    }


    public <T> T castPayload(Class<T> clazz) {
        return (T) (this.payload = GsonUtils.fromJson(text, clazz));
    }

    protected WsMessage(Parcel in) {
        if (in.readByte() == 0) {
            id = null;
        } else {
            id = in.readLong();
        }
        from = in.readString();
        to = in.readString();
        text = in.readString();
        if (in.readByte() == 0) {
            date = null;
        } else {
            date = in.readLong();
        }
        info = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if (id == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(id);
        }
        dest.writeString(from);
        dest.writeString(to);
        dest.writeString(text);
        if (date == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(date);
        }
        dest.writeString(info);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<WsMessage> CREATOR = new Creator<WsMessage>() {
        @Override
        public WsMessage createFromParcel(Parcel in) {
            return new WsMessage(in);
        }

        @Override
        public WsMessage[] newArray(int size) {
            return new WsMessage[size];
        }
    };

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Long getDate() {
        return date;
    }

    public void setDate(Long date) {
        this.date = date;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public Object getPayload() {
        return payload;
    }

    public void setPayload(Object payload) {
        this.payload = payload;
    }


    public static WsMessage obtainC2C(String subInfo) {
        WsMessage wsMessage = new WsMessage();
        wsMessage.info = CLIENT_TO_CLIENT_INFO + subInfo;
        wsMessage.from = AppUtils.getSelfAccess();
        wsMessage.to = AppUtils.getLoveAccess();
        wsMessage.date = TimeUtils.currentTime().getCurrentTime();
        wsMessage.id = System.currentTimeMillis();
        return wsMessage;
    }

    public static WsMessage obtainStrongC2C(String subInfo) {
        WsMessage wsMessage = new WsMessage();
        wsMessage.info = CLIENT_TO_CLIENT_STRONG_INFO + subInfo;
        wsMessage.from = AppUtils.getSelfAccess();
        wsMessage.to = AppUtils.getLoveAccess();
        wsMessage.date = TimeUtils.currentTime().getCurrentTime();
        wsMessage.id = System.currentTimeMillis();
        return wsMessage;
    }
}
